package com.hzjh.h5mall.ui.ads

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.frame.JHBaseFragment
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.ext.widget.dialog.JHReviewFailedDialog
import com.hzjh.base.log.JHLogger
import com.hzjh.base.utils.function.toJson
import com.hzjh.base.utils.function.visibleOrGone
import com.hzjh.h5mall.bean.AdOrderStatus
import com.hzjh.h5mall.bean.AdOrderSubStatus
import com.hzjh.h5mall.bean.UpFileTypes
import com.hzjh.h5mall.bean.ads.AdsTaskDetailBean
import com.hzjh.h5mall.databinding.FragmentAdsOrderDetailBinding
import com.hzjh.h5mall.model.ads.AdsModel
import com.hzjh.h5mall.myinterface.AdsOrderI

@Route(path = RouteUrl.ADS_ORDER_FRAGMENT_DETAIL)
class AdsOrderDetailFragment : JHBaseFragment<FragmentAdsOrderDetailBinding, AdsModel>() {
    private var adsOrderI: AdsOrderI? = null
    private lateinit var adsTaskDetailBean: AdsTaskDetailBean
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentAdsOrderDetailBinding {
        return FragmentAdsOrderDetailBinding.inflate(inflater, container, false)
    }

    override fun startObserve() {
        viewModel.upLoadImgUrl.observe(this){
            when(it.type){
                UpFileTypes.ADVERTISEMENT->{
                    viewBinding.rvAdvertise.addImageUrl(it.url)
                }
                UpFileTypes.SCREENSHOT->{
                    viewBinding.rvScreenshot.addImageUrl(it.url)
                }
                UpFileTypes.OTHER->{
                    viewBinding.rvOthers.addImageUrl(it.url)
                }
            }
            adsOrderI?.changeBtnStatus()
        }
        viewModel.adsSubmitResult.observe(this){
            //提交审核成功
            adsTaskDetailBean.state=0
            changeTaskStatus()
            adsOrderI?.switchNextPage()
        }
    }

    override fun initData() {
        viewBinding.etLinkUrl.setText(adsTaskDetailBean.url)
        viewBinding.etViews.setText("${if(adsTaskDetailBean.realNum<=0) "" else adsTaskDetailBean.realNum}")
        viewBinding.rvAdvertise.setData(adsTaskDetailBean.advertisement)
        viewBinding.rvScreenshot.setData(adsTaskDetailBean.screenshoentt)
        viewBinding.rvOthers.setData(adsTaskDetailBean.others)
        viewBinding.llOtherLayout.visibleOrGone(!(adsTaskDetailBean.state==1&&viewBinding.rvOthers.getData().isEmpty()))
        changeTaskStatus()
    }

    override fun initView(view: View) {
        adsOrderI=activity as AdsOrderI
        viewBinding.llReviewFailed.setOnClickListener {
            //审核失败原因查看
            activity?.let {
                JHReviewFailedDialog.Builder(it)
                    .setContent(adsTaskDetailBean.problems)
                    .create()
                    .show()
            }
        }

        viewBinding.etLinkUrl.addTextChangedListener { text ->
            adsOrderI?.changeBtnStatus()
        }
        viewBinding.etViews.addTextChangedListener { text ->
            adsOrderI?.changeBtnStatus()
        }

        viewBinding.rvAdvertise.setJHImageUpViewI{ filePath ->
            //上传图片
            viewModel.uploadImage(filePath, UpFileTypes.ADVERTISEMENT)
        }
        viewBinding.rvScreenshot.setJHImageUpViewI{ filePath ->
            //上传图片
            viewModel.uploadImage(filePath, UpFileTypes.SCREENSHOT)
        }
        viewBinding.rvOthers.setJHImageUpViewI{ filePath ->
            //上传图片
            viewModel.uploadImage(filePath, UpFileTypes.OTHER)
        }
    }

    private fun changeTaskStatus(){
        //状态，0待审核，1通过，2驳回，99拒绝
        when(adsTaskDetailBean.state){
            AdOrderSubStatus.WAITING.value->{
                viewBinding.llReviewFailed.visibleOrGone(false)
            }
            AdOrderSubStatus.APPROVED.value->{
                viewBinding.etLinkUrl.isEnabled=false
                viewBinding.etViews.isEnabled=false
                viewBinding.rvAdvertise.disableUpload()
                viewBinding.rvScreenshot.disableUpload()
                viewBinding.rvOthers.disableUpload()
            }
            AdOrderSubStatus.NOT_APPROVED.value,
            AdOrderSubStatus.REJECT.value->{
                viewBinding.llReviewFailed.visibleOrGone(adsTaskDetailBean.adsMainStatus!=99){
                    //审核失败原因查看
                    viewBinding.tvFailTime.text=adsTaskDetailBean.updateTime
                }
                viewBinding.llFailedReason.visibleOrGone(adsTaskDetailBean.adsMainStatus==99){
                    viewBinding.tvReasonTime.text=adsTaskDetailBean.updateTime
                    viewBinding.tvContentReason.text = adsTaskDetailBean.problems
                    viewBinding.llReviewFailed.visibleOrGone(false)
                    viewBinding.llUploadContent.visibleOrGone(false)
                }

            }
        }
        if(adsTaskDetailBean.adsMainStatus== AdOrderStatus.CANCEL.value
            ||adsTaskDetailBean.adsMainStatus==AdOrderStatus.TIMEOUT.value
            ||adsTaskDetailBean.adsMainStatus==AdOrderStatus.REJECT.value){
            //主状态是取消状态
            viewBinding.llUploadContent.visibleOrGone(false)
        }
    }

    fun checkNotEmpty():Boolean{
        return viewBinding.etLinkUrl.text.isNotEmpty()
                &&viewBinding.etViews.text.isNotEmpty()&&viewBinding.etViews.text.toString().toFloat()>0
                &&viewBinding.rvAdvertise.getData().isNotEmpty()
                &&viewBinding.rvScreenshot.getData().isNotEmpty()
                &&adsTaskDetailBean.state!=1 //审核通过状态 不能提交
    }

    fun submitPlatformData(){
        //获取平台数据
        val url=viewBinding.etLinkUrl.text.toString()
        val views=viewBinding.etViews.text.toString()
        val advertisementList=viewBinding.rvAdvertise.getData()
        val screenshotList=viewBinding.rvScreenshot.getData()
        val otherList=viewBinding.rvOthers.getData()
        JHLogger.d("---advertisementList图片地址：${advertisementList.toJson()}")
        JHLogger.d("---screenshotList图片地址：${screenshotList.toJson()}")
        JHLogger.d("---otherList图片地址：${otherList.toJson()}")

        adsTaskDetailBean.url=url
        adsTaskDetailBean.realNum=views.toFloat()
        adsTaskDetailBean.advertisement=advertisementList
        adsTaskDetailBean.screenshoentt=screenshotList
        adsTaskDetailBean.others=otherList

        viewModel.reqAdsSubmit(adsTaskDetailBean)

    }

    //设置数据
    fun setData(adsTaskDetailBean: AdsTaskDetailBean){
        this.adsTaskDetailBean=adsTaskDetailBean
    }
}